﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Lion.WeiXin.Core.Data
{
   public class WeChatMenuDAO :IWeChatMenu
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_WeChat_Menu");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12)
			};
            parameters[0].Value = code;

            return SqlHelper.Exists(strSql.ToString(), parameters);
        }      

        public List<Models.WeChatMenuModel> GetMenu()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select AID,Code,Name,Type,Url,Sort,MenuKey ");
            strSql.Append(" FROM T_WeChat_Menu ");
            //if (strWhere.Trim() != "")
            //{
            //    strSql.Append(" where " + strWhere);
            //}
            DataSet ds = SqlHelper.Query(strSql.ToString());
            return ConvertToList(ds);
        }


        public Models.WeChatMenuModel GetModel(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 AID,Code,Name,Type,Url,Sort,MenuKey  from T_WeChat_Menu ");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12)
			};
            parameters[0].Value = code;

            Models.WeChatMenuModel model = new Models.WeChatMenuModel();
            DataSet ds = SqlHelper.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return ConvertToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }



        protected List<Models.WeChatMenuModel> ConvertToList(DataSet ds)
        {
            List<Models.WeChatMenuModel> list = new List<Models.WeChatMenuModel>();
            foreach (DataRow row in ds.Tables[0].Rows)
            {
                Models.WeChatMenuModel model = ConvertToModel(row);
                list.Add(model);
            }
            return list;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Models.WeChatMenuModel ConvertToModel(DataRow row)
        {
            Models.WeChatMenuModel model = new Models.WeChatMenuModel();
            if (row != null)
            {
                if (row["AID"] != null && row["AID"].ToString() != "")
                {
                    model.AID = int.Parse(row["AID"].ToString());
                }
                if (row["Code"] != null)
                {
                    model.Code = row["Code"].ToString();
                }
                if (row["Name"] != null && row["Name"].ToString() != "")
                {
                    model.Name = row["Name"].ToString();
                }
                if (row["MenuKey"] != null)
                {
                    model.Key = row["MenuKey"].ToString();
                }
                if (row["Type"] != null)
                {
                    model.Type = row["Type"].ToString();
                }
                if (row["Sort"] != null && row["Sort"].ToString() != "")
                {
                    model.Sort = int.Parse(row["Sort"].ToString());
                }
                if (row["Url"] != null && row["Url"].ToString() != "")
                {
                    model.Url = row["Url"].ToString();
                }                
            }
            return model;
        }

        public int Create(Models.WeChatMenuModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_WeChat_Menu(");
            strSql.Append("Code,Name,Type,Url,Sort,MenuKey)");
            strSql.Append(" values (");
            strSql.Append("@Code,@Name,@Type,@Url,@Sort,@MenuKey)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Type", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,255),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@MenuKey",SqlDbType.VarChar,50)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Type;
            parameters[3].Value = model.Url;
            parameters[4].Value = model.Sort;
            parameters[5].Value = model.Key;

            object obj = SqlHelper.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        public int Update(Models.WeChatMenuModel model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_WeChat_Menu set ");
            strSql.Append("Code=@Code,");
            strSql.Append("Name=@Name,");
            strSql.Append("Type=@Type,");
            strSql.Append("Url=@Url,");
            strSql.Append("Sort=@Sort,");
            strSql.Append("MenuKey=@MenuKey");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Type", SqlDbType.VarChar,50),
					new SqlParameter("@Url", SqlDbType.VarChar,255),
					new SqlParameter("@Sort", SqlDbType.Int,4),
					new SqlParameter("@MenuKey",SqlDbType.VarChar,50)};
            parameters[0].Value = model.Code;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Type;
            parameters[3].Value = model.Url;
            parameters[4].Value = model.Sort;
            parameters[5].Value = model.Key;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }

        public int Delete(string code)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_WeChat_Menu ");
            strSql.Append(" where Code=@Code");
            SqlParameter[] parameters = {
					new SqlParameter("@Code", SqlDbType.VarChar,12)
			};
            parameters[0].Value = code;

            int rows = SqlHelper.ExecuteSql(strSql.ToString(), parameters);
            return rows;
        }


       
    }
}
